/**
 * @fileOverview Controller modul of Counter widget MVC file
 * 
 * @author Adam Hořčica horcicaa@gmail.com
 * @url $HeadURL: http://ahs-gadgets.googlecode.com/svn/tags/bb-counters/1.0/src/counter_controller.js $
 * @revision $Id: counter_controller.js 47 2008-02-09 20:30:13Z horcicaa $
 */

/**
 * Controller modul of Counter widget MVC
 *  
 * @constructor
 * @param {number} id Counter ID
 * @param {DOMElement} destination Destionation for widget's GUI DOM elements
 * @param {number} n Counter index (position in list)     
 */ 
function CounterController(id, destination, n)
{
  /** Counter ID */
  this.id = id;
  /** Default name */
  this.name = $TPL("__MSG_counter_default_name__", {id: id});
  /** Default detail state */
  var state = StateController.getState(id);
  
  /** View Modul */
  this.view = new CounterView(this, this.name, state, n);
  /** Model Modul */
  this.model = new CounterModel(this, id);

  $.a(destination, this.view.generateDOM());
  this.view.setLabel("__MSG_loading_text__");
  this.model.requestForData();
}

CounterController.prototype = {

  /**
   * Update view date from model (event from Model)
   */     
  updateData: function()
  {
    this.view.setData(this.model.getData());
  },
  
  /**
   * Change state of detail (event from View)
   * @param {boolean} state State of detail    
   */     
  changeState: function(state)
  {
    this.view.setState(state);
    StateController.setState(this.id, state);
    HightController.resize();
  },
  
  /**
   * Error handler
   */     
  showError: function(msg)
  {
    this.view.setLabel(msg);
  }
  
};
